package com.squareup.timessquare;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.jiyici.R;
import com.squareup.timessquare.CalendarCellViewTemp.OnCellClickedListener;

public class YearView extends LinearLayout implements OnCellClickedListener {

    private TextView title;
    private View btnPrev;
    private View btnNext;
    private ViewFlipper viewFlipper;
    private CalendarGridView grid1;
    private CalendarGridView grid2;
    private int year;
    private int firstMode;
    
    private Animation animLeftIn;
    private Animation animLeftOut;
    private Animation animRightIn;
    private Animation animRightOut;

    public YearView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater();
    }

    public YearView(Context context) {
        super(context);
        inflater();
    }

    public void setYear(int year) {
        if (this.year == 0)
            firstMode = year % 2;
        int prevYear = this.year;
        this.year = year;
        title.setText(Integer.toString(year));
        setCountForYear(year);
        CalendarGridView currentGrid = getGridForYear(year);
        if (prevYear == 0) {
            return;
        }
        if (prevYear < year) {
            viewFlipper.setInAnimation(animLeftIn);
            viewFlipper.setOutAnimation(animLeftOut);
            if (currentGrid == grid1)
                viewFlipper.showPrevious();
            else
                viewFlipper.showNext();
        } else {
            viewFlipper.setInAnimation(animRightIn);
            viewFlipper.setOutAnimation(animRightOut);
            if (currentGrid == grid1)
                viewFlipper.showPrevious();
            else
                viewFlipper.showNext();
        }
    }
    
    private void setCountForYear(int year) {
        CalendarGridView grid = getGridForYear(year);
        int childCount = grid.getChildCount();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        title.setText(getYearFormat().format(calendar.getTime()));
        int column = getColumn();
        for (int i = 1; i < childCount; i++) {
            CalendarRowView row = (CalendarRowView) grid.getChildAt(i);
            for (int j = 0; j < column; j++) {
                CalendarCellViewLayout cell = (CalendarCellViewLayout) row.getChildAt(j);
                cell.setCount(year);
                int month = (i - 1) * column + j + 1;
                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR, year);
                c.set(Calendar.MONTH, Calendar.JANUARY + month - 1);
                cell.setCalendar(c);
            }
        }
    }
    
    private CalendarGridView getGridForYear(int year) {
        return year % 2 == firstMode ? grid1 : grid2;
    }
    
    private void showPrevYear() {
        setYear(year - 1);
    }

    private void showNextYear() {
        setYear(year + 1);
    }

    private SimpleDateFormat getYearFormat() {
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.getDefault());
        return yearFormat;
    }

    private SimpleDateFormat getMonthFormat() {
        SimpleDateFormat monthFormat = new SimpleDateFormat("MMM", Locale.getDefault());
        return monthFormat;
    }

    private void inflater() {
        View view = inflate(getContext(), R.layout.year, this);
        title = (TextView) view.findViewById(R.id.title);
        btnPrev = view.findViewById(R.id.btn_prev);
        btnPrev.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showPrevYear();
            }
        });
        btnNext = view.findViewById(R.id.btn_next);
        btnNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showNextYear();
            }
        });
        gestureDetector = new GestureDetector(getContext(), new MyGestureListener());
        viewFlipper = (ViewFlipper) view.findViewById(R.id.view_fliper);
//        viewFlipper.setOnTouchListener(new OnTouchListener() {
//            
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return gestureDetector.onTouchEvent(event);
//            }
//        });
        int column = getColumn();
        grid1 = (CalendarGridView) view.findViewById(R.id.calendar_grid1);
        grid1.setColumn(column);
        grid2 = (CalendarGridView) view.findViewById(R.id.calendar_grid2);
        grid2.setColumn(column);
        setupGrid(grid1);
        setupGrid(grid2);
        
        animLeftIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in);
        animLeftOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out);
        animRightIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_in);
        animRightOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_out);
    }

    private void setupGrid(CalendarGridView grid) {
        int column = getColumn();
        int childCount = grid.getChildCount();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat monthFormat = getMonthFormat();
        for (int i = 1; i < childCount; i++) {
            CalendarRowView row = (CalendarRowView) grid.getChildAt(i);
//            row.setListener(this);
            row.setColumn(column);
            for (int j = 0; j < column; j++) {
                CalendarCellViewLayout cell = (CalendarCellViewLayout) row.getChildAt(j);
                cell.setOnCellClickedListener(this);
                cell.setSelectable(true);
                cell.setCurrentMonth(true);
                int month = (i - 1) * column + j + 1;
                calendar.set(Calendar.MONTH, Calendar.JANUARY + (month - 1));
                cell.setText(monthFormat.format(calendar.getTime()));
                cell.setCount(0);
            }
        }
    }

    private int getColumn() {
        return 4;
    }

    @Override
    public void clickCalendar(Calendar calendar) {
        if (calendar == null)
            return;
        Toast.makeText(getContext(), "" + calendar.get(Calendar.YEAR) +" " + calendar.get(Calendar.MONDAY), Toast.LENGTH_SHORT).show();
    }

    private GestureDetector gestureDetector;
    
    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1.getX() - e2.getX() > 60) {
                showNextYear();
                return true;
            } else if (e1.getX() - e2.getX() < -60) {
                showPrevYear();
                return true;
            }
            return false;
        }
        
    }
}
